//
//  XGPagingView.swift
//  TodayNews
//
//  Created by monkey on 2020/2/22.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import JXSegmentedView

/// 分组菜单高度
let kSectionMenuViewHeight: CGFloat = 50
/// pagingView索引改变通知
let kPagingViewSelectedIndexNotification = Notification.Name(rawValue: "kPagingViewSelectedIndexNotification")

class XGPagingView: UIView
{
    // MARK: 构造方法
    
    /// 导航控制器
    var nav: UINavigationController? {
        didSet {
            // listContainerView支持左滑
            if let pan = nav?.panGestureRecognizer {
                listContainerView.scrollView.panGestureRecognizer.require(toFail: pan)
            }
        }
    }
    /// 顶部tap
    var topTabs: [XGTopTab]? {
        didSet {
            let titles = topTabs?.compactMap({$0.show_name})
            segmentedDataSource.titles = titles ?? []
            segmentedView.reloadData()
        }
    }
    
    /// 选中某页
    func selectItemAt(_ index: Int) -> Void
    {
        segmentedView.selectItemAt(index: index)
    }
    
    init(userId: UInt64)
    {
        self.userId = userId
        super.init(frame: CGRect.zero)
        setUpUI()
    }
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 私有属性
    
    /// 指示器
    private lazy var segmentedView = JXSegmentedView()
    /// 容器
    private lazy var listContainerView = JXSegmentedListContainerView(dataSource: self, type: .scrollView)
    /// 指示器数据源 必须属性持有
    private let segmentedDataSource: JXSegmentedTitleDataSource = {
        let dataSource = JXSegmentedTitleDataSource()
        dataSource.isTitleColorGradientEnabled = true
        return dataSource
    }()
    /// 用户id
    private var userId: UInt64 = 0
    
    // MARK: 内部方法
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        segmentedView.frame = CGRect(x: 0, y: 0, width: fmax(bounds.width, TodayNews_Screen_Width), height: kSectionMenuViewHeight)
        listContainerView.frame = CGRect(x: 0, y: segmentedView.frame.maxY, width: bounds.width, height: bounds.height - segmentedView.bounds.height)
    }
    
    /// 设置界面
    private func setUpUI() -> Void
    {
        // 配置指示器
        segmentedView.delegate = self
        segmentedView.dataSource = segmentedDataSource
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = JXSegmentedViewAutomaticDimension
        indicator.lineStyle = .lengthenOffset
        indicator.verticalOffset = 3
        segmentedView.indicators = [indicator]
        // 配置容器
        segmentedView.listContainer = listContainerView
        backgroundColor = UIColor.white
        addSubview(segmentedView)
        addSubview(listContainerView)
    }
}

// MARK: - JXSegmentedViewDelegate

extension XGPagingView: JXSegmentedViewDelegate
{
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int)
    {
        // 第一页时才可以左滑退出
        nav?.panGestureRecognizer.isEnabled = index == 0
        NotificationCenter.default.post(name: kPagingViewSelectedIndexNotification, object: index, userInfo: nil)
    }
}

// MARK: - JXSegmentedListContainerViewDataSource

extension XGPagingView: JXSegmentedListContainerViewDataSource
{
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int
    {
        return topTabs?.count ?? 0
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate
    {
        let type = topTabs![index].type
        let viewController = XGRetweetStatusViewController(userId: userId, type: type)
        if index > 0 {
            let previousTableView = listContainerView.validListDict[index - 1]?.listView() as? UITableView
            viewController.tableView.isScrollEnabled = previousTableView?.isScrollEnabled ?? false
        }
        return viewController
    }
}
